/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.user.shared;

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * An extension of {@link Map}<{@code String},{@code Object}>, expects value in types of
 * {@link String}, {@link Number}, {@link Boolean}, {@link SimpleObject} and {@link Collection}.
 * this simple object gets and sets value just by property name and can be serialized to json
 * easily.
 * 
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
public final class SimpleObject {
  // TODO fast map impl
  private Map<String, Object> map = new HashMap<String, Object>();

  /**
   * Creates a simple object.
   */
  public SimpleObject() {
  }

  /**
   * Creates a simple object with the given map.
   * 
   * @param map the map
   */
  public SimpleObject(Map<String, Object> map) {
    this.map = map;
  }

  /**
   * Gets the value of key.
   * 
   * @param key the key
   * @return the value of key.
   */
  public Object get(String key) {
    return map.get(key);
  }

  /**
   * Retrieves the expected boolean value.
   * 
   * @param fieldName the field name
   * @return the expected boolean value
   */
  public boolean getBoolean(String fieldName) {
    Object value = map.get(fieldName);
    if (value == null) {
      return false;
    }
    return (Boolean) value;
  }

  /**
   * Retrieves a copy of this.
   * 
   * @return Retrieves a copy of this.
   */
  public SimpleObject getCopy() {
    return new SimpleObject(new HashMap<String, Object>(map));
  }

  /**
   * Retrieves date value that convert from number(long).
   * 
   * @param fieldName the field name
   * @return the date value that convert form number(long)
   */
  public Date getDate(String fieldName) {
    Object value = map.get(fieldName);
    if (value == null) {
      return null;
    }
    return new Date(((Number) value).longValue());
  }

  /**
   * Retrieves int value that convert from number.
   * 
   * @param fieldName the field name
   * @return the int value that convert form number
   */
  public int getInt(String fieldName) {
    Object value = map.get(fieldName);
    if (value == null) {
      return 0;
    }
    return ((Number) value).intValue();
  }

  /**
   * Retrieves the expected list value.
   * 
   * @param fieldName the field name
   * @return the expected list value
   */
  @SuppressWarnings("unchecked")
  public <T> List<T> getList(String fieldName) {
    return (List<T>) map.get(fieldName);
  }

  /**
   * Retrieves the expected number value.
   * 
   * @param fieldName the field name
   * @return the expected number value
   */
  public Number getNumber(String fieldName) {
    return (Number) map.get(fieldName);
  }

  /**
   * Retrieves the expected SimpleObject value.
   * 
   * @param fieldName the field name
   * @return the expected SimpleObject value.
   */
  public SimpleObject getSimpleObject(String fieldName) {
    return (SimpleObject) map.get(fieldName);
  }

  /**
   * Retrieves the expected string value.
   * 
   * @param fieldName the field name
   * @return the expected string value
   */
  public String getString(String fieldName) {
    return (String) map.get(fieldName);
  }

  /**
   * Checks whether a null value exist.
   * 
   * @return whether a null value exist.
   */
  public boolean hasNull() {
    return map.values().contains(null);
  }

  /**
   * Returns true if this map contains no key-value mappings
   * 
   * @return true if this map contains no key-value mappings
   */
  public boolean isEmpty() {
    return map.isEmpty();
  }

  /**
   * Returns the key set.
   * 
   * @return the key set
   */
  public Set<String> keySet() {
    return map.keySet();
  }

  /**
   * Sets a value for field name.
   * 
   * @param fieldName the field name
   * @param value the value
   */
  public Object put(String fieldName, Object value) {
    return map.put(fieldName, value);
  }

  @Override
  public String toString() {
    return "SimpleObject [map=" + map + "]";
  }

  /**
   * Returns all values.
   * 
   * @return all values
   */
  public Collection<Object> values() {
    return map.values();
  }
}
